<?php

namespace app\index\controller;

use app\BaseController;
use app\index\model\Article;
use app\index\model\Comment as ModelComment;
use app\index\model\Msg;
use app\index\model\Tap;
use app\index\model\User;
use think\facade\Request;
use think\facade\Session;
use think\model\concern\SoftDelete;

class Comment extends Base
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    public function list()
    {
        $this->logs('评论列表');
        $param = input();
        $id = @$param['id'];
        $page = @$param['page'] == '' ? 1 : @$param['page'];
        if (!is_numeric($page)) return error('页数输入有误');
        $data = (new Article())->getArticleData($id);
        if (!$data) return error('该文章已丢失');
        $commentList = (new ModelComment())->list($id, $page);
        return success($commentList);
    }
    public function send()
    {
        $this->logs('发送评论');
        if (Request::isPost()) {
            $param = input();
            $pid = $param['pid'];
            $article_id = $param['id'];
            if ($pid == '') return error('缺少参数Pid');
            if ($article_id == '') return error('文章ID不能为空');
            $art_data = Article::where('id', $article_id);
            if ($art_data->findOrempty($article_id)->isEmpty()) return error('文章不存在');
            if ($param['comment'] == '') return error('评论内容不能为空');
            $art_data = $art_data->find();
            if ($pid != '0') {
                // 不存在这个评论或文章已被删除
                $cmt = ModelComment::where('id', $pid);
                if ($cmt->withTrashed()->findOrempty()->isEmpty()) return error('错误提交');
                $cmt = $cmt->find();
                // if($art_data->type == 3 && $art_data->user_id != $cmt->user_id && $cmt->user_id != $this->user['user_id']) return error('由于文章类型限制，只能回复楼主或自己的评论哦！');
                // 发送消息
                $pidUserData = ModelComment::with(['userInfo' => function ($query) {
                    $query->field('id , username');
                }])->withTrashed()->where('id', $pid)->find();
                $username = $pidUserData->userInfo->nickname;
                $uid =  $pidUserData->userInfo->id;
                if ($uid != $this->user['user_id']) {
                    $msg =  $username . '回复你的评论[' . $pidUserData->comment . ']：' . $param['comment'];
                    $paramArr = ['article_id' => $article_id, 'type' => 1, 'content' => $msg];
                    Msg::send($this->user['user_id'], $uid, $paramArr);
                }
            }
            $author_id = $art_data->user_id;
            if ($this->user['user_id'] != $author_id) {
                $msg =  $this->user['nickname'] . '回复你的文章[' . $art_data->title . ']：' . $param['comment'];
                $paramArr = ['article_id' => $article_id, 'type' => 1, 'content' => $msg];
                Msg::send($this->user['user_id'], $author_id, $paramArr);
            }
            if ($art_data->status != 1) return error('文章处于不允许评论状态');
            $data = [
                'comment' => $param['comment'],
                'pid' => $pid,
                'article_id' => $article_id,
                'user_id' => $this->user['user_id'],
            ];
            $res = ModelComment::create($data);
            if ($res) return success('发送成功');
            return error('发送失败');
        }
    }
    public function del()
    {
        $this->logs('删除评论');
        $id = input('id');
        $hasCom = ModelComment::where(['id' => $id])->find();
        if ($hasCom) {
            $isAuthor = Article::where(['id' => $hasCom->article_id, 'user_id' => $this->user['user_id']])->findOrEmpty()->isEmpty();
            // 站长可操作删除所有评论
            $role = Session::get('user.role');
            if (!$isAuthor || $hasCom || $role == 'all') {
                $res = ModelComment::destroy($id);
                if ($res) return success();
                else  return error('删除失败');
            }
        }
        return error();
    }
}
